package com.ruoyi.web.controller.my;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.question_details.domain.QuestionDetails;
import com.ruoyi.question_details.service.IQuestionDetailsService;
import com.ruoyi.system.domain.QuestionText;
import com.ruoyi.system.service.IQuestionTextService;
import com.ruoyi.texts.domain.Text;
import com.ruoyi.texts.service.ITextService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.my.domain.Question;
import com.ruoyi.my.service.IQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试题Controller
 *
 * @author ruoyi
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/my/question")
public class QuestionController extends BaseController
{
    @Autowired
    private IQuestionDetailsService questionDetailsService;
    @Autowired
    private ITextService textService;
    @Autowired
    private IQuestionTextService questionTextService;
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询试题列表
     */
//    @PreAuthorize("@ss.hasPermi('my:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(Question question)
    {
        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }

    /**
     * 导出试题列表
     */
    @PreAuthorize("@ss.hasPermi('my:question:export')")
    @Log(title = "试题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question)
    {
        List<Question> list = questionService.selectQuestionList(question);
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "试题数据");
    }

    /**
     * 获取试题详细信息
     */
    @PreAuthorize("@ss.hasPermi('my:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionService.selectQuestionById(id));
    }

    /**
     * 新增试题
     */
    @PreAuthorize("@ss.hasPermi('my:question:add')")
    @Log(title = "试题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Question question)
    {
        question.setCreateTime(DateUtils.getNowDate());
        question.setCreateUser(SecurityUtils.getUserId().toString());
        question.setUpdateUser(SecurityUtils.getUserId().toString());
        question.setUpdateTime(DateUtils.getNowDate());
        List<Text> texts = textService.selectTextList(null);
        questionService.insertQuestion(question);
        List<Question> questions = questionService.selectQuestionList(null);
        Long questionId = questions.get(questions.size()-1).getId();
        QuestionDetails questionDetails = new QuestionDetails();
        questionDetails.setQuestionId(questionId);
        questionDetails.setDescription("");
        questionDetails.setChoose("");
        questionDetailsService.insertQuestionDetails(questionDetails);
        return toAjax(true);
    }

    /**
     * 修改试题
     */
    @PreAuthorize("@ss.hasPermi('my:question:edit')")
    @Log(title = "试题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Question question)
    {
        System.err.println(question);
        question.setUpdateTime(DateUtils.getNowDate());
        question.setUpdateUser(SecurityUtils.getUserId().toString());
        return toAjax(questionService.updateQuestion(question));
    }

    /**
     * 删除试题
     */
    @PreAuthorize("@ss.hasPermi('my:question:remove')")
    @Log(title = "试题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(questionService.deleteQuestionByIds(ids));
    }


    @GetMapping("/getQuestionByUserId")
    public AjaxResult getQuestionByUserId()
    {
        return success(questionService.selectQuestionList(null));
    }

    @PostMapping("/addQuestion")
    public AjaxResult addQuestion(@RequestBody Map map)
    {
        System.err.println(map);
        Question question = new Question();
        Object name = map.get("name");
        Object key = map.get("key");
        List<String> list=new ArrayList<>();
        List<String> chooseList=new ArrayList<>();
        Object a = map.get("A");
        chooseList.add("A");
        list.add(a.toString());
        Object b = map.get("B");
        chooseList.add("B");
        list.add(b.toString());
        chooseList.add("C");
        Object c = map.get("C");
        list.add(c.toString());
        chooseList.add("D");
        Object d = map.get("D");
        list.add(d.toString());
        question.setCreateUser(SecurityUtils.getUserId().toString());
        question.setScore(2l);
        question.setName(name.toString());
        question.setUserId(SecurityUtils.getUserId());
        question.setCreateTime(DateUtils.getNowDate());
        question.setUpdateTime(DateUtils.getNowDate());
        question.setKey11(key.toString());
        question.setStatus("1");
        System.err.println(question);
        question.setUpdateUser(SecurityUtils.getUserId().toString());
        questionService.insertQuestion(question);
        List<Question> questions = questionService.selectQuestionList(null);
        Long questionId = questions.get(questions.size()-1).getId();
        for (int i = 0; i < 4; i++) {
            QuestionDetails questionDetails = new QuestionDetails();
            questionDetails.setChoose(chooseList.get(i));
            questionDetails.setDescription(list.get(i));
            questionDetails.setQuestionId(questionId);
            questionDetailsService.insertQuestionDetails(questionDetails);
        }
        return null;
    }
}
